//保存表格的实例对象
var sx = null;

//调用封装方法
getTable();


// ---------------------------------------------------
//方法区

//封装方法重载表格
function sxTable(){
    //通过表格id参数  重载刷新
    sx.reload()
}


//封装渲染表格
function getTable(){
    layui.use(['table','layer','jquery','form',],function() {
        //模板保存到变量上
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var $ = layui.$;


        sx = table.render({
            elem:"#demo",
            url:"/YltUser/UserManage/getZeroMonsy",
            page:{
                layout:['count','prev','page','next','limit'],
                groups:5,
                limit:10,
                limits:[5,10,20]
            },
            cols:
                [[
                    {checkbox:true},
                    // {field: 'courseLogId', title: '序号',width: 100,align: 'center'},
                    {field: 'userId', title: '用户编号',width: 150,align: 'center',templet:function (d){
                            return d.user.userId;
                        }},
                    {field: 'userAccount', title: '用户账号', width:200,align: 'center',templet:function (d){
                            return d.user.userAccount;
                        }},
                    {field: 'userName', title: '用户昵称',width: 200,align: 'center',templet:function (d){
                            return d.user.userName;
                        }},
                    {field: 'userCreateDate', title: '用户注册时间', width: 200,align: 'center',templet:function (d){
                            return d.user.userCreateDate;
                        }}
                ]],
            toolbar:'#toolbardemo',
            skin: 'line',
            parseData:function (res){
                return{
                    "code":res.code,
                    "msg":res.msg,
                    "count":res.count,
                    "data":res.data
                }
            }

        }),



        //监听表格头部
        table.on('toolbar(filterDemo)',function (obj){
            var event = obj.event
            var checkStatus = table.checkStatus(obj.config.id);
            var data =  checkStatus.data;
            // console.log(event)
            // console.log(checkStatus)
            // console.log(data)

            if (obj.event == 'gx'){
                if (data.length == 0){
                    layer.msg('至少选择一位用户',{icon:5,time:1000})
                }else {
                    var array = [];
                    $.each(data,function (){
                        // console.log(this)
                        array.push(this.user.userId)
                    })
                    console.log(array)
                    layer.open({
                        type:2,
                        title:'勾选优惠券',
                        content:'couponTable.html',
                        shade:[1,'#37444B'],
                        anim:4,
                        area:['100%','100%'],
                        //弹出层成功弹出后的回调函数
                        success:function (layero,index) {
                            //父页面向子页面传参 -- 向iframe层传数据
                            //获取iframe页面
                            var body = layer.getChildFrame('body', index);
                            body.find('#userArray').val(array.toString());
                        },
                        end:function (){
                            let index = parent.layer.getFrameIndex(window.name);
                            //再执行关闭
                            parent.layer.close(index);
                            //调用自己写的重载方法
                            parent.sxTable();
                        }
                    })
                }
            }
        })
    })

}